import { isArray } from 'lodash-es'
import { useUserStore } from '../stores/modules/user';

export function checkAuth(value: Array<string>) {
  if (value && value instanceof Array && value.length > 0) {
    const permissions = useUserStore().user.permissions
    const permissionDatas = value

    const hasPermission = permissions.some(permission => {
      return permissionDatas.includes(permission)
    })

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    return false
  }
}

// 判断是否有指定的用户权限
export const hasOneOfPermissions = (permissions: string[] = []) => {
  const userPermissions = useUserStore().user.permissions
  if (userPermissions && userPermissions.length) {
    if (isArray(permissions)) {
      return permissions.some(key => userPermissions.includes(key as never))
    } else {
      return false
    }
  } else {
    return false
  }
}
